package com.sise.thread.Lock;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 作者：idea
 * 日期：2018/6/25
 * 描述：测试独占锁
 */
public class TestMutex {

   public List list=new ArrayList();
   public Lock lock=new Mutex();

   public void insert(){
       try {
           lock.lock();
           for(int i=0;i<100;i++){
               list.add(i);
               Thread.sleep(10);
           }
           System.out.println(list.size());
       }catch (Exception e){
            e.printStackTrace();
       }finally {
           lock.unlock();
       }
   }

    public static void main(String[] args) {
        final  TestMutex t=new TestMutex();
        new Thread(){
            public void run(){
                t.insert();
            }
        }.start();
        new Thread(){
            public void run(){
                t.insert();
            }
        }.start();

    }




}
